var Project = {
    tableId: "projectTable"
};

function log(msg) {
    setTimeout(() => {
        throw new Error(msg);
    }, 0);
}

var numberPage = 1;

layui.use(['table', 'admin', 'ax', 'form', 'layer', 'jquery','func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var layer = layui.layer;
    var func = layui.func;

    /* leez */
    var windowClientWidth = document.body.clientWidth
    var windowClientWidthTemp = document.body.clientWidth
    var tableIsReloading = false

    function _reloadTable() {
        if(tableIsReloading) return
        tableIsReloading = true
        windowClientWidth = document.body.clientWidth;
        if(Math.abs(windowClientWidth - windowClientWidthTemp) > 20) {
            windowClientWidthTemp = windowClientWidth
        }
        setTimeout(() => {
            tableIsReloading = false
        }, 1500)
    }

    /**
     * 初始化表格的列
     */
    Project.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {type: 'numbers', title: '序号', align: "center", width: 50, minWidth: '40', hide: false},
            {field: 'name', title: '店名', minWidth: '170', align: "left",templet(d) {
                return '<div><span style="color:#d9d6d6;">'+d.provinceName+'</span></br><span>'+d.name+'</span></div>';
                }},
            {field: 'zlArea',  title: '租赁面积㎡', minWidth: '70', align: "center"},
            {field: 'waibaiArea',  title: '外摆区面积㎡', minWidth: '80', align: "center"},
            {field: 'ad',  title: '广告位个数', minWidth: '70', align: "center",templet(d) {
                var n=0;
                if(d.ad1!=null && d.ad1!=''){
                    n++;
                }
                if(d.ad2!=null && d.ad2!=''){
                    n++;
                }
                if(d.ad3!=null && d.ad3!=''){
                    n++;
                }
                if(d.ad4!=null && d.ad4!=''){
                    n++;
                }
                return n;
            }},
            {field: 'rent', title: '<div>租赁<br/>周期(年)</div>', minWidth: '70', align: "center",templet(d) {
                    var n=0;
                    if(d.rent1!=''&&d.rent1!=null){
                        n++;
                    }
                    if(d.rent2!=''&&d.rent2!=null){
                        n++;
                    }
                    if(d.rent3!=''&&d.rent3!=null){
                        n++;
                    }
                    if(d.rent4!=''&&d.rent4!=null){
                        n++;
                    }
                    if(d.rent5!=''&&d.rent5!=null){
                        n++;
                    }
                    if(d.rent6!=''&&d.rent6!=null){
                        n++;
                    }
                    if(d.rent7!=''&&d.rent7!=null){
                        n++;
                    }
                    if(d.rent8!=''&&d.rent8!=null){
                        n++;
                    }
                    if(d.rent9=''&&d.rent9!=null){
                        n++;
                    }
                    if(d.rent10!=''&&d.rent10!=null){
                        n++;
                    }
                    return n;
                }},
            {field: 'mztime', title: '<div>免租<br/>周期(月)</div>', minWidth: '150', align: "center",templet(d){
                if(d.mzTimeday==null || d.mzTimeday==''){
                    return d.mzTime;
                }else {
                    var dd= toDecimal(d.mzTimeday/30);
                    return toDecimal(d.mzTime)+dd;
                }
                return d.mzTime;
                }},
            {field: 'rent',  title: '租金', minWidth: '500', align: "center",templet:'#rentTpl'},
            {field: 'ccxs',  title: '抽成%', minWidth: '70', align: "center"},
            {field: 'waterFee', title: '<div>水费<br/>(元/吨)</div>', minWidth: '70', align: "center"},
            {field: 'eleFee', title: '<div>电费<br/>(元/度)</div>', minWidth: '70', align: "center"},
            {field: 'wuyeFee', title: '<div>物业费<br/>(元/月)</div>', minWidth: '70', align: "center"},
            {field: 'bzj',  title: '<div>质量保<br/>证金(元)</div>', minWidth: '70', align: "center"},
            {field: 'zlbzj',  title: '<div>租赁保<br/>证金(元)</div>', minWidth: '70', align: "center"},
            {field: 'wybzj',  title: '<div>物业保<br/>证金(元)</div>', minWidth: '70', align: "center"},
            {field: 'kfry',  title: '开发人员', minWidth: '150', align: "center",templet(d){
                    return d.kfry+d.kfryTel;
                }},
            {field: 'zhaoshang', title: '招商负责人', minWidth: '150', align: "center",templet(d){
                    return d.zhaoshang+d.zhaoshangTel;
                }},

            {field: 'kgtime', sort: true, title: '开工时间', minWidth: '100', align: "center"},
            {field: 'opendate', sort: true, title: '开业时间', minWidth: '110', align: "center"},
            {field: 'createTime', sort: true, title: '添加时间', minWidth: '130', align: "center"},
            {field: 'bz', title: '备注', minWidth: '80', align: "left",templet(d) {
                    var v='';
                    if(d.bz!=null && d.bz.length>30){
                        v= d.bz.substring(0,30)+'...';
                    }else{
                        v= d.bz;
                    }
                   var show='<span title="'+d.bz+'">'+ v+'</span>';
                    return show;
                }},
        ]];
    };

    /**
     * 点击查询按钮
     */
    Project.search = function () {
        var queryData = {};
        // queryData['condition'] = $("#condition").val();
        queryData['provinceId'] = $("#provinceName").val();
        queryData['cityId'] = $("#cityName").val();
        queryData['simpleName'] = $("#name").val();
       /* queryData['startWorkYn'] = $("#startWorkYn").val();
        queryData['cashierAccountYn'] = $("#cashierAccountYn").val();
        queryData['allStautusEnd'] = $("#allStautusEnd").val();
        queryData['status'] = $("#status").val();*/
        /*table.reload(Project.tableId, {
            where: queryData, page: {curr: 1}
        });*/
        // Project.tableCfg.url=Feng.ctxPath + '/project/list';
        Project.tableCfg.where=queryData;
        table.render(Project.tableCfg);
        // sessionStorage.setItem("gcglList",JSON.stringify(queryData));
    };

   /* /!**
     * 弹出添加通知
     *!/
    Project.openAddDlg = function () {
        parent.layer.open({
            type: 2,
            title: '新增',
            area: ['90%', '650px'],
            content: Feng.ctxPath + '/project/finishAdd',
            end: function () {
                Project.search();
            },
            cancel:function (index){
                // Mdzx.search();
            }
        });
    };*/

    /**
     * 导出excel按钮
     */
    Project.exportExcel = function () {
        var checkRows = table.checkStatus(Project.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 检索区域
     */
    var qyAjax = new $ax("/area/getAreaByType");
    qyAjax.set("type", 2);
    var qyResult = qyAjax.start();
    $.each(qyResult, function (index, data) {
        $("#provinceName").append("<option value='" + data.id + "'>" + data.simpleName + "</option>");
    });
    form.render('select');
    form.on('select(provinceName)', function (data) {
        var provinceId = data.value;
        if (provinceId == "") {
            $("#cityName").html("");
            $("#cityName").append("<option value=''>全部</option>");
            form.render('select');
        } else {
            var qyAjax = new $ax("/area/getAreaByPid");
            qyAjax.set("pid", provinceId);
            var qyResult = qyAjax.start();
            $("#cityName").html("");
            $("#cityName").append("<option value=''>全部</option>");
            $.each(qyResult, function (index, data) {
                $("#cityName").append("<option value='" + data.id + "'>" + data.simpleName + "</option>");
            });
            form.render('select');
        }
    });

    Project.tableCfg={
        elem: '#' + Project.tableId,
        url:Feng.ctxPath + '/mouldBoardMsg/listDataShangwu',
        // even: true,
        page:true,
        height:document.body.offsetHeight,
        cellMinWidth: 0,
        cols: Project.initColumn(),
        done:function(res, curr, count){
            $(".layui-table-main tr").each(function(index, val) {
                $($(".layui-table-fixed-l .layui-table-body tbody tr")[index]).height($(val).height());
                $($(".layui-table-fixed-r .layui-table-body tbody tr")[index]).height($(val).height());
            })

        },
    };
    table.render(Project.tableCfg);
    /**
     * 搜索按钮点击事件
     */
    $('#btnSearch').click(function () {
        Project.search();
    });

    /**
     * 关店按钮点击事件
     */
    $('#btnEdit').click(function () {
        // $('#address').removeAttr('required');
        Project.editStatus();
    });

    /**
     * 添加按钮点击事件
     */
    $('#btnAdd').click(function () {
        Project.openAddDlg();
    });

    /**
     * 导出excel
     */
    $('#btnExp').click(function () {
        Project.exportExcel();
    });


    /**
     * 工具条点击事件
     */
    table.on('tool(' + Project.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            window.location.href = Feng.ctxPath + '/project/shangwu?id=' + data.id + "&swedit=0&auditbtn=0";
        }
    });



});

function toDecimal(x) {
    var f = parseFloat(x);
    if (isNaN(f)) {
         return 0;
    }
    f = Math.round(x*100)/100;
    return f;
}

function valid(s){
    if(s!=null && s!='' && s!='0'){
        return true;
    }
    return false;
}
